﻿#include "systemfile.h"
//systemFile sy;
systemFile *systemFile::m_ini = NULL;
QMutex systemFile::m_mutex;
systemFile *systemFile::GetInstance()
{
    if (m_ini == NULL) {
            QMutexLocker mutexLocker(&m_mutex);;  // 自解锁
            if (m_ini == NULL) {
                m_ini = new systemFile();
            }
        }
        return m_ini;
}

systemFile::systemFile()
{
//    QString fileName=QString("%1/settings.ini").arg(QDir::currentPath());

//    this->devSetting = new QSettings(fileName,QSettings::IniFormat);
//    if(QFile::exists(fileName) == false)
//    {
//        this->resetSetting();
//    }else{
      //  this->readSetting();
  //  }
    fileName=QString("/home/bgjsource/settings.ini").arg(QDir::currentPath());
    qDebug()<<"fileName="<<fileName;
    //this->readSetting();
    //读取MAC地址
    QNetworkInterface  localInterface =localInterface.interfaceFromName("eth0");
    this->mac =localInterface.hardwareAddress();
    qDebug() <<"mac address:"<< this->mac;


}
//读取系统设置
bool systemFile::readSetting()
{
    mutex.lock();
    this->devSetting = new QSettings(fileName,QSettings::IniFormat);
    if(QFile::exists(fileName) == false)
        resetSetting();
    this->devSetting->setIniCodec(QTextCodec::codecForName("UTF-8"));

    this->devSetting->beginGroup("SERVER");
    //this->serverIp=this->devSetting->setValue();
    this->serverIp = this->devSetting->value("serverIp").toString();
    this->userName = this->devSetting->value("userName").toString();
    this->passWord = this->devSetting->value("passWord").toString();
    this->databaseName = this->devSetting->value("databaseName").toString();
    this->serverPort = this->devSetting->value("serverPort").toInt();
    this->ftpDir = this->devSetting->value("ftpDir").toString();
    this->devSetting->endGroup();

    this->devSetting->beginGroup("UDP");
    this->udpIp = this->devSetting->value("udpIp").toString();
    this->udpSendPort = this->devSetting->value("udpSendPort").toInt();
    this->udpRecivePort = this->devSetting->value("udpRecivePort").toInt();
    this->devSetting->endGroup();

    //读取磅表设置
    this->devSetting->beginGroup("WEIGHT");
    this->weightBaudRate = this->devSetting->value("BaudRate").toInt();
    this->weightType = this->devSetting->value("Type").toString();

    this->weightModel = this->devSetting->value("Model").toString();
    this->weightNum = this->devSetting->value("Number").toInt();
    this->devSetting->endGroup();
    //读取磅表设置
    this->devSetting->beginGroup("AXIS");
    this->axisBaudRate = this->devSetting->value("BaudRate").toInt();
    this->axisTime=this->devSetting->value("axisTime").toInt();
    this->axisDev=this->devSetting->value("axisDev").toString();
    this->axisValue=this->devSetting->value("axisVal").toInt();
    this->axisMin=this->devSetting->value("axisMin").toInt();
    this->axisMax=this->devSetting->value("axisMax").toInt();
    this->devSetting->endGroup();
    //语音板配置
    this->devSetting->beginGroup("AUDIO");
    this->audioBaudRate = this->devSetting->value("BaudRate").toInt();
    this->devSetting->endGroup();
    //服务器webservice配置
    this->devSetting->beginGroup("WEB");
    this->webAction=this->devSetting->value("webAction").toString();
    this->webHost=this->devSetting->value("webHost").toString();
    this->webPort=this->devSetting->value("webPort").toInt();
    this->webMethod=this->devSetting->value("webMethod").toString();
    this->webMethodUrl=this->devSetting->value("webMethodUrl").toString();
    this->webMethodKey=this->devSetting->value("webMethodKey").toString();
    this->webMethodType=this->devSetting->value("webType").toString();
    this->webUrlPath=this->devSetting->value("webUrlPath").toString();
    this->sectionId=this->devSetting->value("sectionId").toString();
    this->sectionType=this->devSetting->value("sectionType").toString();
    this->devSetting->endGroup();

    this->devSetting->beginGroup("LED");
    this->ledIp1=this->devSetting->value("ledIp1").toString();
    this->ledIp2=this->devSetting->value("ledIp2").toString();
    this->devSetting->endGroup();

    this->devSetting->beginGroup("DEVICE");
    this->galleryName = this->devSetting->value("galleryName").toString();
    this->galleryNumber = this->devSetting->value("galleryNumber").toString();
    this->regNumber = this->devSetting->value("regNumber").toString();
    this->url = this->devSetting->value("url").toString();
    this->devSetting->endGroup();
    qDebug()<<"******DEVICE********";
    qDebug()<<"galleryName**"<<this->galleryName;
    qDebug()<<"galleryNumber"<<this->galleryNumber;
    qDebug()<<"regNumber****"<<this->regNumber;   
    qDebug()<<"******SERVER********";
    qDebug()<<"serverIp*****"<<this->serverIp;
    qDebug()<<"userName*****"<<this->userName;
    qDebug()<<"passWord*****"<<this->passWord;
    qDebug()<<"databaseName*"<<this->databaseName;
    qDebug()<<"serverPort***"<<this->serverPort;
    qDebug()<<"ftpDir*******"<<this->ftpDir;
    qDebug()<<"******UDP********";
    qDebug()<<"udpIp********"<<this->udpIp;
    qDebug()<<"udpSendPort**"<<this->udpSendPort;
    qDebug()<<"udpRecivePort**"<<this->udpRecivePort;
    qDebug()<<"******WEIGHT*****";
    qDebug()<<"BaudRate********"<<this->weightBaudRate;
    qDebug()<<"Type************"<<this->weightType;
    qDebug()<<"Model***********"<<this->weightModel;
    qDebug()<<"Number*************"<<this->weightNum;
    qDebug()<<"******AXIS*****";
    qDebug()<<"BaudRate********"<<this->axisBaudRate;
    qDebug()<<"axisTime********"<<this->axisTime;
    qDebug()<<"axisDev********"<<this->axisDev;
    qDebug()<<"axisVal********"<<this->axisValue;
    qDebug()<<"axisMin********"<<this->axisMin;
    qDebug()<<"axisMax********"<<this->axisMax;
    qDebug()<<"******AUDIO*****";
    qDebug()<<"BaudRate********"<<this->audioBaudRate;
    qDebug()<<"******WEB********";
    qDebug()<<"webAction******"<<this->webAction;
    qDebug()<<"webHost********"<<this->webHost;
    qDebug()<<"webPort********"<<this->webPort;
    qDebug()<<"webMethod******"<<this->webMethod;
    qDebug()<<"webMethodUrl***"<<this->webMethodUrl;
    qDebug()<<"webMethodKey***"<<this->webMethodKey;
    qDebug()<<"webMethodType**"<<this->webMethodType;
    qDebug()<<"webUrlPath*****"<<this->webUrlPath;
    qDebug()<<"sectionId******"<<this->sectionId;
    qDebug()<<"sectionType****"<<this->sectionType;
    qDebug()<<"******LED********";
    qDebug()<<"LedIp1*********"<<this->ledIp1;
    qDebug()<<"LedIp2*********"<<this->ledIp2;

    if(weightType.contains("静态"))
        weightType="1";
    if(weightType.contains("动态"))
        weightType="2";
    if(axisDev.contains("软件"))
        axisDev="0";
    if(axisDev.contains("光栅"))
        axisDev="1";
    if(axisDev.contains("地磁"))
        axisDev="2";
    if(weightModel.contains("XK3190-A9"))
        weightModel="1";
    if(weightModel.contains("WT3190"))
        weightModel="2";
    if(weightModel.contains("TLD8142"))
        weightModel="3";

    delete devSetting;
    if(devSetting != NULL)
        devSetting=NULL;

    mutex.unlock();
    return true;
}
bool systemFile::resetSetting()
{
    fileName=QString("%1/settings.ini").arg(QDir::currentPath());
    QString fileInfo;
    QFile myfile(fileName);
    qDebug() << "*********************************";
    qDebug() << "reset setting";
    qDebug() << "*********************************";
    myfile.open(QIODevice::WriteOnly);
    //
    fileInfo.append("[DEVICE]\n");
    fileInfo.append("galleryName =设备1\n");
    fileInfo.append("galleryNumber = 1\n");
    fileInfo.append("regNumber = 00000000\n");
     fileInfo.append("url = \n");
    fileInfo.append("ftpDir = /media/sda\n");
    //
    fileInfo.append("[SERVER]\n");
    fileInfo.append("serverIp = 192.168.0.233\n");
    fileInfo.append("userName = root\n");
    fileInfo.append("passWord = hahaha\n");
    fileInfo.append("databaseName = bgj\n");
    fileInfo.append("serverPort = 3306\n");
    //
    fileInfo.append("[UDP]\n");
    fileInfo.append("udpIp =192.168.0.195\n");
    fileInfo.append("udpSendPort =5000\n");

    //称重磅表
    fileInfo.append("[WEIGHT]\n");
    fileInfo.append("BaudRate = 9600\n"); //波特率
    fileInfo.append("Type = 1\n");        //磅表类型: 1静态 2动态
    fileInfo.append("Model = 2\n");       //磅表型号

    //轮轴识别
    fileInfo.append("[AXIS]\n");
    fileInfo.append("BaudRate = 9600\n"); //波特率
    fileInfo.append("Type = 1\n");        //类型: 1静态 2动态
    fileInfo.append("Model = 1\n");       //型号

    //语音
    fileInfo.append("[AUDIO]\n");
    fileInfo.append("BaudRate = 9600\n"); //波特率

    //WEB
    fileInfo.append("[WEB]\n");
    fileInfo.append("webAction = http://bangfang.net/GetData\n"); //设置动作
    fileInfo.append("webHost = 47.94.83.242\n");//设置主机IP
    fileInfo.append("webPort = 8082\n"); //设置主机端口
    fileInfo.append("webMethod = GetData\n");//设置方法函数
    fileInfo.append("webMethodUrl = http://bangfang.net/\n"); //设置方法URL
    fileInfo.append("webMethodKey = f4eea7d428a071e8e9602ec3f6edc3\n");//授权码
    fileInfo.append("webMethodType = 1\n"); //数据类型称重记录出入1，设备注册传入6，设备日志传入7
    fileInfo.append("webUrlPath = /WebService.asmx\n");//数据请求webPath
    fileInfo.append("sectionId = 1\n"); //数据类型称重记录出入1，设备注册传入6，设备日志传入7
    fileInfo.append("sectionType = 源头企业\n");//数据请求webPath

    myfile.write(fileInfo.toLocal8Bit());
    myfile.close();
    this->ftpDir="/home/root";
    this->udpIp="192.168.10.233";
    this->udpSendPort=5000;
    //
    this->weightBaudRate=9600;
    this->axisBaudRate=9600;
    this->audioBaudRate=9600;
    //
    this->webAction="http://bangfang.net/GetData";
    this->webHost="47.94.83.242";
    this->webPort=8082;
    this->webMethod="GetData";
    this->webMethodUrl="http://bangfang.net/";
    this->webMethodKey="0351 - 8383873";
    this->webMethodType="1";
    this->webUrlPath="/WebService.asmx";
    this->sectionId="140204B111116";
    //this->sectionType="源头企业";
    return true;
}

bool systemFile::rereadSettting()
{
    //sy.readSetting();
    qDebug()<<"bool systemFile::rereadSettting()";

    if(this->readSetting())
        return true;
    else
        return false;
}

